# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl utility.t'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use strict;
use warnings;
use utility;

use Test::More tests => 1;
BEGIN { use_ok('utility') }

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.
my $util         = new utility;
my $op_text      = undef;
my $DBServerName = "carmd-app28";
my $DBName       = "mfgt_db_Lab";

# Test 1
$util->logmsg( $op_text, "Test message" );

# Test 2
my $dbh = $util->connect_to_db_server_mssql( $DBServerName, $DBName );

# Test 3
my $sqlScript = 'select * from dbo.TBL_GENERAL_DATA 
where G_DUT_Type like \'SL8081\'
and  G_App_Software_Version like \'R1-2-7\'';

my $res = $util->run_query( $dbh, $sqlScript );
my $record_H;
foreach $record_H (@$res) {
	# $util->logmsg( $op_text, join( ",", @$record_H ) );
}

# Test 4
my $res_AoH = $util->run_query_hash( $dbh, $sqlScript );
my $record_Ref_H;
my %restultHoA;
foreach $record_Ref_H (@$res_AoH) {
	while ( my ( $key, $value ) = each(%$record_Ref_H) ) {
		# print "$key => $value\n";
		# push @{ $restultHoA{$key} }, $value;

		#print "$key => $record_Ref_H->{$key} \n";

	}

	#	$util -> logmsg($op_text, $key.$_{$key});

	# $util->logmsg( $op_text, join( ",", @$res_AoH ) );
	# my @values = map{$record_Ref_H->{$_}} %$record_Ref_H;
	# $util->logmsg( $op_text, join( ",", @values ) );
}

my $type_info_all = $dbh->type_info_all;

foreach my $idx ( 0 .. scalar @{$type_info_all} - 1 ) {
	if ( $idx == 0 ) {
		my @keysA;
		my @valueA;
		while ( my ( $key, $value ) = each( %{ $type_info_all->[0] } ) ) {
			push @keysA,  $key;
			push @valueA, $value;
		}
		$util->logmsg( $op_text, join( ",", @keysA ) );
		$util->logmsg( $op_text, join( ",", @valueA ) );
		next;
	}
	# $util->logmsg( $op_text, join( ",", @{ $type_info_all->[$idx] } ) );

}
#$util->logmsg( $op_text, $type_info_all );

my $sth =
  $dbh->column_info( '', $DBName, 'dbo.TBL_GENERAL_DATA', '%' );
#$util->logmsg( $op_text, $type_info_all );

# Test 5
# $util->GenRChart_Boxplot( 	\undef,
# 							\"c:/tmp", 
# 							\"c:/tmp/Testtime_Boxplot_datafile.txt",
#							\"c:/tmp/Boxplot.png" );

# Test 6
$util->GenRChart_Pareto(\"c:/tmp", 
						\"c:/tmp/ParetoChart.txt",
						\"c:/tmp/ParetoChart.png" );

# Test 7
my @IndividualDataSetA;
my @dataset;

push @IndividualDataSetA, 42;
#push @IndividualDataSetA, "c:/tmp/42-TC_GSMTxTest...Curr_HighPA_P-limits.txt";
push @IndividualDataSetA, "c:/tmp/15-SFT...GSM_850_GMSK_FreqErrAtMaxPwr_M-limits.txt";
#push @IndividualDataSetA,  "c:/tmp/42-TC_GSMTxTest...Curr_HighPA_P-datasource.txt";
push @IndividualDataSetA,	"c:/tmp/15-SFT...GSM_850_GMSK_FreqErrAtMaxPwr_M-datasource.txt";
#push @IndividualDataSetA,	"c:/tmp/42-TC_GSMTxTest...Curr_HighPA_P-datasource.png";
push @IndividualDataSetA,	"c:/tmp/15-SFT...GSM_850_GMSK_FreqErrAtMaxPwr_M-datasource.png";
push @dataset, \@IndividualDataSetA;
$op_text = undef;
# $util->GenRChart_ProcessCapability( \$op_text, \"c:/tmp", \"chartType_Obsolete",
#	\@dataset );

# Test 8
my @buildsummaryAoA;
my $tmp = 20;
push @buildsummaryAoA, [ 'Yield_1st+2nd', $tmp . '%' ];
push @buildsummaryAoA, [ 'Qty Tested',    $tmp ];
push @buildsummaryAoA, [ 'Overall Yield', $tmp . '%' ];

my @ECbreakdownAoA;
push @ECbreakdownAoA, [
	'ErrorCode-01',
	'30%',    # $No_Of_Unique_FSN_1st,
	'ErrorCode-02',
	'20%'
];            # $No_Of_Unique_FSN_1st * 100 / $No_Of_Unique_FSN_1st ];
push @ECbreakdownAoA, [
	'Total',
	'50',    # $No_Of_Unique_FSN_1st,
];  

my $ParetoChart_PNGFile = "./ParetoChart.png";

# build arrays for web page.
my @itemlizeddataAoAref;
push @itemlizeddataAoAref, [
	12,               #$i,
	150,              #$lsl,
	550,              #$usl,
	'mA',             #$unit,
	'CurrentTest',    #$TestCaseName,
	'./42-TC_GSMTxTest...Curr_HighPA_P-datasource.png'
];
my $outputfile = "C:/tmp/main.html";
# $util->GenHtmlFile( 
# 					\"C:/carmd-cvs02/Koala", 
#					\@buildsummaryAoA,
#					\@ECbreakdownAoA,
#					
#					\$ParetoChart_PNGFile, 
#					\@itemlizeddataAoAref, 
#					\$outputfile );



# test 9


my $table_info = $dbh->table_info('', '', '');
print "table infor: $table_info";


END {

	# Close the database
	$dbh->disconnect() if ( ref($dbh) );
}



# Test 10
my @inputArray = ["abc","abc","abc","def","def","def","def"];
my @uniqueArray = $util->return_unique(@inputArray);
